<?php

namespace App\Http\Controllers\General\File;

use App\Http\Controllers\Controller;
use App\Http\Controllers\General\File\Traits\UploadTrait;
use App\Services\Oss\OssService;
use Illuminate\Http\Request;

class OssController extends Controller
{
    use UploadTrait;

    //
    public function directPolicyUploadImage(Request $request, OssService $ossService){
        $originalFileName=$request->original_name??'';
        if(!$originalFileName){
            return $this->fail('原文件名为必填项');
        }
        $extension=pathinfo($originalFileName,PATHINFO_EXTENSION);
        if(!in_array(strtolower($extension),$this->imageTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }

        $relativePath='storage/direct/' . 'image' . '/'.date('Ym').'/'.date('d');
        $fileName=$this->getRandomFileName($extension);
        $uploadPolicy=$ossService->directUploadPolicy($relativePath,$fileName);

        if($request->noid != 1){
            $this->createUploadLog($request->user()->id,$originalFileName,'image','',$uploadPolicy['oss_file_url']);
        }

        return $this->success(['upload_policy'=>$uploadPolicy]);
    }


    //
    public function directPolicyUploadFile(Request $request, OssService $ossService){
        $originalFileName=$request->original_name??'';
        if(!$originalFileName){
            return $this->fail('原文件名为必填项');
        }
        $extension=pathinfo($originalFileName,PATHINFO_EXTENSION);
        if(!in_array(strtolower($extension),$this->fileTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }

        $relativePath='storage/direct/' . 'file' . '/'.date('Ym').'/'.date('d');
        $fileName=$this->getRandomFileName($extension);
        $uploadPolicy=$ossService->directUploadPolicy($relativePath,$fileName);
        if($request->noid != 1){
            $this->createUploadLog($request->user()->id,$originalFileName,'file','',$uploadPolicy['oss_file_url']);
        }

        return $this->success(['upload_policy'=>$uploadPolicy]);
    }
    //
    public function uploadImageByServer(Request $request, OssService $ossService){
        $image = $request->file('image');
        if (!$image->isValid()) {
            return $this->fail('上传失败，上传文件无效');
        }
        if(!in_array($image->getClientOriginalExtension(),$this->imageTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }
        $size=round($image->getSize()/1024/1024,2);
        if($size>5){
            return $this->fail('上传失败，上传文件大小不能超过5MB');
        }
        $relativePath='storage/server/' . 'image' . '/'.date('Ym').'/'.date('d');
        $ossFileName=$relativePath.'/'.$this->getRandomFileName($image->getClientOriginalExtension());
        $webPath=$ossService->storeOssFile($image->getRealPath(),$ossFileName);
        if($request->noid != 1){
            $this->createUploadLog($request->user()->id,$image->getClientOriginalName(),'image','',$webPath);
        }

        return $this->success(['web_path'=>$webPath]);
    }

    //
    public function uploadFileByServer(Request $request, OssService $ossService){
        $file = $request->file('file');
        if (!$file->isValid()) {
            return $this->fail('上传失败，上传文件无效');
        }
        if(!in_array($file->getClientOriginalExtension(),$this->fileTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }
        $relativePath='storage/server/' . 'file' . '/'.date('Ym').'/'.date('d');
        $ossFileName=$relativePath.'/'.$this->getRandomFileName($file->getClientOriginalExtension());
        $webPath=$ossService->storeOssFile($file->getRealPath(),$ossFileName);
        if($request->noid != 1){
            $this->createUploadLog($request->user()->id,$file->getClientOriginalName(),'file','',$webPath);
        }

        return $this->success(['web_path'=>$webPath]);
    }


}
